package com.woniuxy.company5.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.company5.dto.UserCustomerDto;
import com.woniuxy.company5.entity.UserCustomer;
import com.woniuxy.company5.service.UserCustomerService;
import com.woniuxy.company5.util.Result;
import com.woniuxy.company5.vo.UserCustomerVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/userCustomer")
public class UserCustomerController {
    @Resource
    private UserCustomerService userCustomerService;
    @PostMapping("query")
    public Result queryByLike(@RequestBody UserCustomerDto userCustomerDto){
        PageHelper.startPage(userCustomerDto.getCurrentPage(),userCustomerDto.getPageSize(),true);
        List<UserCustomerVo> UserCustomer = userCustomerService.selectByLike(userCustomerDto);
        PageInfo<UserCustomerVo> pageInfo = new PageInfo<>(UserCustomer);
        return Result.ok().data("userList",UserCustomer).data("total",pageInfo.getTotal());
    }
    @PostMapping("")
    public Result add(@RequestBody UserCustomer userCustomer){
        userCustomerService.insertSelective(userCustomer);
        return Result.ok().data("uid",userCustomer.getId());
    }
    @GetMapping("{id}")
    public Result queryById(@PathVariable("id") Long id){
        UserCustomer userCustomer = userCustomerService.selectByPrimaryKey(id);
        return Result.ok().data("user",userCustomer);
    }
    @PutMapping("")
    public Result update(@RequestBody UserCustomer userCustomer){
        userCustomerService.updateByPrimaryKeySelective(userCustomer);
        return Result.ok();
    }
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id){
        userCustomerService.deleteByPrimaryKey(id);
        return Result.ok();
    }

}
